package com.neuedu.dao.impl;

import com.neuedu.dao.HuoDongDao;
import com.neuedu.model.HuoDong;
import com.neuedu.model.XiaoQu;
import com.neuedu.utils.DbUtil;

import java.util.ArrayList;
import java.util.List;

public class HuoDongDaoImpl implements HuoDongDao {
    @Override
    public List<HuoDong> selectPage(String minDate, String maxDate, String likeName, Integer limitParam1, Integer limitParam2) {
        //chuanjianshijian>=minDate
        //chuangjianshijian<=MaxDate
        //mingcheng like "%likeName%"
        String where = "1=1 ";
        List<Object> params = new ArrayList<>();
        if (minDate != null && !minDate.equals("")) {
            where = where + "and huodongkaishishijian >= ? ";
            params.add(minDate);
        }
        if (maxDate != null && !maxDate.equals("")) {
            where = where + "and huodongjiezhishijian <= ? ";
            params.add(maxDate);
        }
        if (likeName != null && !likeName.equals("")) {
            where = where + "and xiaoqvmingcheng like ? ";
            params.add("%"+likeName+"%");
        }
//        if (where.length() > 3) {
//            where = where.substring(3);
//        }

        where = where + "order by id desc limit ?,? ";
        params.add(limitParam1);
        params.add(limitParam2);

        List<HuoDong> result = DbUtil.select(HuoDong.class, where, params.toArray());
        return result;
    }

    @Override
    public int count(String minDate, String maxDate, String likeName) {
        String where = "1=1 ";
        List<Object> params = new ArrayList<>();
        if (minDate != null && !minDate.equals("")) {
            where = where + "and chuangjianshijian >= ? ";
            params.add(minDate);
        }
        if (maxDate != null && !maxDate.equals("")) {
            where = where + "and chuangjianshijian <= ? ";
            params.add(maxDate);
        }
        if (likeName != null && !likeName.equals("")) {
            where = where + "and suoshuxiaoqv like ? ";
            params.add("%"+likeName+"%");
        }

        List<HuoDong> result = DbUtil.select(HuoDong.class, where, params.toArray());
        return result==null?0:result.size();
    }

    @Override
    public int insertHuoDong(HuoDong huoDong) {
        return DbUtil.insertSelective(huoDong);
    }

    @Override
    public int deleteHuoDong(Integer[] ids) {
        String where ="id in (";
        for (int i = 0; i < ids.length; i++) {
            where=where+"?";
            if(i!=ids.length-1){
                where=where+",";
            }else{
                where=where+")";
            }
        }
        return DbUtil.delete(HuoDong.class,where,ids);
    }

    @Override
    public HuoDong selectHuoDongById(Integer id) {
        List<HuoDong> list = DbUtil.select(HuoDong.class, "id=?", id);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateHuoDong(HuoDong huoDong) {
        return DbUtil.update(huoDong,"id=?",huoDong.getId());
    }


}
